import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
  'user',
  () => {
    const user = ref({})
    const setUser = (val) => {
      user.value = val
    }
    // 清除用户信息
    const clearUser = () => {
      user.value = {}
    }

    // 跳转页面和跳转方式记录在这里
    // 记录登录成功后的跳转页面的地址
    const redirectURL = ref('/pages/index/index')
    // 记录页面跳转的方式
    const openType = ref('switchTab')

    return {
      user,
      setUser,
      clearUser,
      redirectURL,
      openType,
    }
  },
  {
    persist: {
      storage: {
        setItem: (key, value) => {
          uni.setStorageSync(key, value)
        },
        getItem: (key) => {
          return uni.getStorageSync(key)
        },
      },
    },
  }
)
